package org.jetnuke.util;

import org.apache.log4j.Logger;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletContext;

public class GetResources {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(GetResources.class);

	private GetResources() {
	};

	public static List getResources(ServletContext sc, String initPath) {
		if (logger.isDebugEnabled()) {
			logger.debug("getResources(ServletContext, String) - start"); //$NON-NLS-1$
		}

		List resList = null;
		String path = sc.getRealPath(initPath);
		File f = new File(path);
		String strs[] = f.list();
		resList = new ArrayList(Arrays.asList(strs));

		if (logger.isDebugEnabled()) {
			logger.debug("getResources(ServletContext, String) - end"); //$NON-NLS-1$
		}
		return resList;
	}

	public static List<String> getDirectories(ServletContext sc, String initPath) {
		if (logger.isDebugEnabled()) {
			logger.debug("getDirectories(ServletContext, String) - start"); //$NON-NLS-1$
		}

		List<String> resList = null;
		String path = sc.getRealPath(initPath);
		File f = new File(path);
		String strs[] = f.list();
		resList = new ArrayList<String>();
		if (strs != null) {
			for (int i = 0; i < strs.length; i++) {
				File dir = new File(path, strs[i]);
				if (dir.isDirectory())
					System.out.println(strs[i]);
					resList.add(strs[i]);
			}
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getDirectories(ServletContext, String) - end"); //$NON-NLS-1$
		}
		return resList;
	}

	public static List getFileNames(ServletContext sc, String initPath) {
		if (logger.isDebugEnabled()) {
			logger.debug("getFileNames(ServletContext, String) - start"); //$NON-NLS-1$
		}

		List resList = null;
		String path = sc.getRealPath(initPath);
		File f = new File(path);
		String strs[] = f.list();
		resList = new ArrayList();
		if (strs != null) {
			for (int i = 0; i < strs.length; i++) {
				File dir = new File(path, strs[i]);
				if (!dir.isDirectory())
					resList.add(strs[i]);
			}
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getFileNames(ServletContext, String) - end"); //$NON-NLS-1$
		}
		return resList;
	}

	public static List getFiles(ServletContext sc, String initPath) {
		if (logger.isDebugEnabled()) {
			logger.debug("getFiles(ServletContext, String) - start"); //$NON-NLS-1$
		}

		List resList = null;
		String path = sc.getRealPath(initPath);
		File f = new File(path);
		File files[] = f.listFiles();
		resList = new ArrayList();
		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				if (!files[i].isDirectory())
					resList.add(files[i]);
			}
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getFiles(ServletContext, String) - end"); //$NON-NLS-1$
		}
		return resList;
	}
}
